package Video
{
	import SplineComponent.SplineComponentModel;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	import mx.events.SliderEvent;
	import mx.events.VideoEvent;
	
	public class VideoModel extends SplineComponentModel
	{
		private var lessonModel_object:Object = new Object;
		private var lessonView_object:Object = new Object;
		private var lessonEditMode_bool:Boolean;
		
		private var view:VideoView;
		private var controller:VideoController;
		public var loader:URLLoader;
		public var request:URLRequest;
		public var playListArray:Array=new Array;;
		public var videoTime:Number;
		public var videoFile:File;
		public var fileName:String;
		public var fileNameArray:Array=new Array;
		public var sourcePathArray:Array=new Array;
		public var sourcePath:String;
		public var listIndex:Number=0;
		public function VideoModel(lessonModel:Object, lessonView:Object,lessonPath:String, lessonEditMode:Boolean)
		{
			super();
			this.lessonModel_object = lessonModel;
			this.lessonView_object = lessonView;
			this.lessonEditMode_bool=lessonEditMode;
			this.controller = new VideoController(this);
			this.view= new VideoView(this,this.controller,this.lessonView_object,this.lessonEditMode_bool);
			this.addObserver(this.view);
			
			
		}
		
		public function evtListner_loadComplete(evt:Event):void
		{
			
		}
		
		
		public function playVideo():void
		{
			
			
			if(playListArray.length==0)
			{
				Alert.show("PLEASE SELECT SOME FILE TO PLAY");
				return;
			}
			if(!view.videoDisplayContainer.playing)
			{
				var infoObj:Object=new Object;
				infoObj.message="lessonPlayedFromStreamingComponents";
				this.sendInfoToLessonModel(infoObj);
				
				view.playButton.setStyle("icon",view.pauseIcon);
				view.playButton.toolTip="PAUSE";
				view.videoDisplayContainer.play();
				videoTime=view.videoDisplayContainer.totalTime;
				//view.navigationSlider.liveDragging=true;
				
				trace(videoTime);
				view.seekBarLabel.text=0+"/"+(videoTime/60);
				view.navigationSlider.maximum=videoTime;
				view.videoDisplayContainer.addEventListener(VideoEvent.PLAYHEAD_UPDATE,evtListner_playHeadUpdate);
				view.navigationSlider.addEventListener(SliderEvent.CHANGE,evtListner_navigate);
				
			}
			else if(view.videoDisplayContainer.playing) 
			{
				var infoObj:Object=new Object;
				infoObj.message="lessonPausedFromStreamingComponents";
				this.sendInfoToLessonModel(infoObj);
				
				view.playButton.setStyle("icon",view.playIcon);
				view.playButton.toolTip="PLAY";
				view.videoDisplayContainer.pause();
				
			}
		} 
		
		
		public function evtListner_playHeadUpdate(evt:VideoEvent):void
		{
			
			var videoTimeInMinutes:Number=Math.round((view.videoDisplayContainer.playheadTime/60)*100)/100;
			view.seekBarLabel.text=videoTimeInMinutes.toString()+"/"+Math.round((videoTime/60)*10)/10;
			view.navigationSlider.value=view.videoDisplayContainer.playheadTime;	
		}
		
		
		public function stopVideo():void
		{
			if(playListArray.length==0)
			{
				Alert.show("THERE IS NO FILE TO BE STOPPED");
				return;
			}
			view.playButton.setStyle("icon",view.playIcon);
			view.playButton.toolTip="PLAY";
			view.videoDisplayContainer.autoRewind=true;
			view.videoDisplayContainer.stop();
			
		}
		

		public function evtListner_navigate(evt:SliderEvent):void
		{
			
			view.videoDisplayContainer.playheadTime=view.navigationSlider.value;
		}
		
		public function openFileList():void
		{
			videoFile=new File;
			var videoType:FileFilter=new FileFilter("Video(*.flv)","*.flv");
			var videoTypeArray:Array=new Array(videoType);
			videoFile.addEventListener(Event.SELECT,evtListner_browseComplete);
			videoFile.browse(videoTypeArray);
		}
		
		public function evtListner_browseComplete(evt:Event):void
		{
			sourcePathArray.push(evt.target.url);
	  		fileNameArray.push(evt.target.name);
	        
	        sourcePath=sourcePathArray[listIndex];
	        fileName=fileNameArray[listIndex];
	        
	        view.videoDisplayContainer.source = sourcePath;
	        
	        view.playButton.setStyle("icon",view.playIcon);
	        playListArray.push(fileName);
	        view.playList.dataProvider=playListArray;
	        view.playList.selectedItem=fileNameArray[listIndex];
	        view.videoDisplayContainer.load();
	        
	        listIndex+=1;
		}
		
		
		public function onPlayListClicked():void
		{
			view.playButton.setStyle("icon",view.playIcon);
			var index:Number=view.playList.selectedIndex;
			trace("anks"+index);
			view.videoDisplayContainer.source=sourcePathArray[index];
			view.videoDisplayContainer.load();
			
		}
		
		
		public function onPlayListDoubleClicked():void
		{
			var index:Number=view.playList.selectedIndex;
			view.videoDisplayContainer.source=sourcePathArray[index];
			view.videoDisplayContainer.load();
			this.playVideo();
			
		}
		
		public function playNextVideo():void
		{
			if(playListArray.length==0)
			{
				Alert.show("PLEASE SELECT SOME FILE TO PLAY");
				return;
			}
			var newIndex:Number;
			if(playListArray.length==1)
			{
				Alert.show("ONLY A SINGLE VIDEO IN YOUR PLAYLIST");
				return;
			}
			
			view.playButton.setStyle("icon",view.playIcon);
			view.playButton.toolTip="PLAY";
			view.videoDisplayContainer.autoRewind=true;
			view.videoDisplayContainer.stop();
			newIndex=view.playList.selectedIndex;
			
			if(newIndex==playListArray.length-1)
			{
				newIndex=0;
				
				
			}
			else
			{
				 newIndex=newIndex+1;
				 
			}
			
			view.videoDisplayContainer.source=sourcePathArray[newIndex];
			view.playList.selectedIndex=newIndex;
			view.videoDisplayContainer.load();
			this.playVideo();
		}
		
		public function playPreviousVideo():void
		{
			if(playListArray.length==0)
			{
				Alert.show("PLEASE SELECT SOME FILE TO PLAY");
				return;
			}
			var newIndex:Number;
			if(playListArray.length==1)
			{
				Alert.show("ONLY A SINGLE VIDEO IN YOUR PLAYLIST");
				return;
			}
			
			view.playButton.setStyle("icon",view.playIcon);
			view.playButton.toolTip="PLAY";
			view.videoDisplayContainer.autoRewind=true;
			view.videoDisplayContainer.stop();
			newIndex=view.playList.selectedIndex;
			if(newIndex==0)
			{
				newIndex=playListArray.length-1;
				
			}
			else
			{
				 newIndex=newIndex-1;
			}
			
			view.videoDisplayContainer.source=sourcePathArray[newIndex];
			view.playList.selectedIndex=newIndex;
			view.videoDisplayContainer.load();
			this.playVideo();
		}
		
		public function removeVideoFromList():void
		{
			view.playButton.setStyle("icon",view.playIcon);
			view.playButton.toolTip="PLAY";
			view.videoDisplayContainer.autoRewind=true;
			view.videoDisplayContainer.stop();
			listIndex-=1;
			sourcePathArray.splice(listIndex,1);
			fileNameArray.splice(listIndex,1);
			playListArray.splice(listIndex,1);
			view.playList.dataProvider=playListArray;
			
			
			
			if(listIndex==0)
			{
				//do nothing
			}
			else
			{
				view.videoDisplayContainer.source=sourcePathArray[playListArray.length-1];
				view.playList.selectedIndex=playListArray.length-1;
				view.videoDisplayContainer.load();
			}
			
		}
		//-------------------------------------Closing function--------------------------------------------------------------------//
		private function closeVideoComponent():void
		{
			var infoObj_object:Object = new Object;
			infoObj_object.message = "close";
			this.setChanged();
			this.notifyObservers(infoObj_object);
		}
		//-------------------------------------------------------------------------------------------------------------------------//
		//------------------------------------Communication Functions----------------------------------------------------------------//
		public function incomingMessageGateway(infoObj_object:Object):void
		{
			var message:String=infoObj_object.message.toString();
			
			switch(message)
			{
				case "mouseUpEvent":
					trace("VideoModel------->event:mouseup  infoObj.event :"+infoObj_object.event);
					this.setChanged();
					this.notifyObservers(infoObj_object);
					break;
					
				case "lessonPlayed":
					this.playVideo();
					break;
					
				case "lessonPaused":
					this.playVideo();
					break;
					
				case "lessonStopped":
					this.stopVideo();
					break;
				
				case "updateCurrentTime":
					//this.view.navigationSlider.value=infoObj_object.time;
					//this.view.videoDisplayContainer.playheadTime=this.view.navigationSlider.value;
					break;	
				case "close":
					this.closeVideoComponent();
					break;
				
				case "saveData":
					break;
			}
		}
		
		private function sendInfoToLessonModel(infoObj_object:Object):void
		{
			this.lessonModel_object.incomingMessageGateway(infoObj_object);
		}
		
	}
}